package com.itheima.netty.c4;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

@Slf4j
public class TestNettyFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();

        EventLoop eventLoop = group.next();

        Future<Integer> future = eventLoop.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.info("执行计算");
                Thread.sleep(1000);
                return 30;
            }
        });
        // 同步
        // log.info("等待结果");
        // Integer integer = future.get();
        // log.info("结果是{}",integer);
        // 异步
        log.info("等待结果");
        //现在Future对象不是ChannelFuture,不能使用ChannelFutureListener监听
        //GenericFutureListener用于监听任何类型的 Future 对象的接口
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.info("结果是{}", future.getNow());
            }
        });
    }
}
